import requests
import uuid

'''
注意事项：
1. 项目名称不能重复
2. 流程名称不能重复
3. 每个程序运行前和运行后执行传递的run_id必须一致，是通过uuid生成的，每次请求都会生成一个新的run_id
'''


class MonitoringTask:
    def add_task(self, task_name):
        """
        添加项目
        :param task_name: 项目名称，不能重复
        :return:
        """
        data = {
            'project_name': task_name
        }
        url = 'http://192.168.66.35:8000/project/'
        response = requests.post(url, json=data)
        print(response.text)

    def get_task(self):
        """
        获取所有项目
        :return:
        """
        url = 'http://192.168.66.35:8000/project/'
        response = requests.get(url)
        print(response.text)

    def add_procedure(self, procedure_name, project_id, run_time):
        """
        添加流程
        :param procedure_name: 程序名称
        :param project_id: 项目id，通过上面的获取所有项目接口获取
        :param run_time: 按照linux的crontab格式进行提交
        :return:
        """
        data = {
            'procedure_name': procedure_name,
            'project_id': project_id,
            'run_time': run_time
        }
        url = 'http://192.168.66.35:8000/procedure/'
        response = requests.post(url, json=data)
        print(response.json())

    ################# 获取所有流程 #################
    def get_procedure(self, project_id):
        """
        获取所有流程
        :param project_id: 项目id，通过上面的获取所有项目接口获取
        :return:
        """
        get_url = 'http://192.168.66.35:8000/procedure/?project_id={}'.format(project_id)
        response = requests.get(get_url)
        print(response.json())

    def timer_before(self, procedure_id):
        """
        定时任务运行前请求
        :param procedure_id: 项目id，通过上面的获取所有项目接口获取
        :return:运行id
        """
        run_id = str(uuid.uuid4())
        data = {
            'procedure_id': f"{procedure_id}",
            'run_id': run_id  # 运行id，这个是自己生成的，用于后面的请求
        }
        url = 'http://192.168.66.35:8000/request_before/'

        response = requests.post(url, json=data)
        print(response.json())
        return run_id

    def timer_after(self, procedure_id, run_id):
        """
        定时任务运行后请求
        :param procedure_id: 流程id，通过上面的获取所有流程接口获取
        :param run_id: 运行id，这个是自己生成的，用于后面的请求
        :return:
        """
        data = {
            'procedure_id': procedure_id,
            'run_id': run_id
        }
        url = 'http://192.168.66.35:8000/request_after/'
        response = requests.post(url, json=data)
        print(response.json())

    def update_procedure(self,procedure_id,procedure_name,run_time):
        data = {
            'procedure_id': procedure_id,
            'procedure_name': procedure_name,
            'run_time': run_time,
        }

        response = requests.put(url="http://192.168.66.35:8000/procedure/", json=data)
        print(response.text)

monitoring_task = MonitoringTask()
if __name__ == '__main__':
    "3"
    # monitoring_task.add_procedure("会议云定时采集",3,"0 18 * * *") # 58
    # monitoring_task.add_procedure("会议云每日推送",3,"20 8 * * *") # 59
    # monitoring_task.update_procedure(59,"会议云每日推送","20 8 * * *") # 60
    "5 知网图书"
    # monitoring_task.add_procedure("知网图书定时采集", 5, "20 6 * * *")  # 58
    # monitoring_task.add_procedure("知网图书定时推送", 5, "20 8 * * *")  # 58
    monitoring_task.update_procedure(65, "知网图书定时采集推送", "20 6 * * *")  # 65

